home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap09
/
b09d005.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
5KB
|
109 lines
0, Do you remember the Lucky Seven program?
4, In this demonstration, I'll revisit the
6, Lucky Seven program to explore the
8, concept of a public variable. I'll add a
11, standard module to Lucky Seven and I'll
13, create a public variable in it named Wins,
16, which keeps track of the number of
17, jackpots I hit when running the slot machine.
21, Let's give it a try. Right now, each
23, time I click the Spin button, a 7 appears,
27, and a jackpot appears, but I have no
29, real way of keeping track of my wins. So,
32, I'll end the program, and I'll go down
34, to the Lucky Seven label, and make it a
37, little smaller since it takes up so much
38, room. Below the Lucky Seven label, I'll
41, create another label, called Wins. In
44, this label, I'll keep track of the number
46, of wins in my program. And I'll click
50, the Properties window to set some
51, properties for the Label5 object. I'll set the
57, Name property to lblWins to identify it
61, in the program code. And I'll set the
65, Alignment property to Center. Next, I'll
70, set the Caption property to Wins. And I'll
75, set the Original Value to 0. And I'll
79, use the Font property to specify Arial,
84, 12-point, bold, italic. And I'll use the
90, ForeColor property to specify a dark
93, green color. Now, I'll change the caption
100, on my form to Lucky Seven, a detail I
106, didn't add in the first chapter, but which
108, makes it look nice. Okay, now that I've
113, finished creating my label on the form,
115, I'm ready to add a standard module to
117, the project. And I'll do that by clicking
120, the Project menu and clicking the Add
122, Module command. Visual Basic displays the
128, Add Module dialog box. And when I click
131, the Open button, Visual Basic adds a new
135, module, named Module1, to my project,
138, and it appears in the Code window. And I
141, can declare my public variable, named
143, Wins, by typing Public Wins in the code
148, module. And Visual Basic will declare a
151, variable named Wins that is of the variant
155, type. That means it can include any
156, type of information in the variable. We'll
159, have an integer number, but it could be
161, any type because it's a variant. And
163, that will be a public variable available to
165, all the event procedures in the
167, program. So I can go ahead and close the
171, Properties window and then open up the Project
175, window. And as you can see, my new
181, module appears in the Modules folder in the
183, Project window. And when I want to save
185, this module to disk, I just need to
187, highlight that module and then use the Save
190, Module As command on the File Menu.
193, Okay, now I want to go back into the first
195, form and add some code that's associated
198, with that Spin button. So, I'll click
200, the form and then click the View Code
202, button. And when the Code window appears, I
206, can begin to add the program code that
207, increments my Wins variable. So I'll get
210, rid of the Project window to add some
212, more space. And immediately below the Beep
215, statement, the place I want to
216, increment my variable because I have a win, I
219, can type the program code, Wins = Wins +
224, 1. And this will increment the Wins
225, variable every time I win. Then I'll go ahead
228, and display that value. I'll type,
230, lblWins.Caption, this is my new label on my
235, form, = "Wins: " , which is added to the
244, Wins variable. So this is the part of
247, the program code that increments the Wins
250, public variable when a 7 appears in a
252, Spin. Now the second statement uses the
254, concatenation operator to assign a
257, string to the lblWins object. And that
260, format is Wins:Number. Okay, and that will
264, increment as my program goes. So now,
267, let's run this and see what it looks like.
270, Okay, I'll click the Start button. Now, I
274, can really keep track of the Lucky
275, Seven program. The first time I click the
277, Spin button, I have a win, and a Win
280, appears in the label I just added. The second
283, time, I have a win. The third time, I
285, have a win, the fourth time, and the
287, fifth time I have a win. Then I go on a bit of a
290, jag, but after a total of ten clicks, I
293, have six wins. The public variable Wins
296, was particularly useful here because it
298, maintained its value through ten calls of
300, the Command1_Click event procedure. Now,
302, if I had declared Wins locally in that
304, procedure, the variable would have reset
306, each time. It's a bit like what a trip
308, odometer in your car does when you push the
310, reset button. But using a public
312, variable in a standard module lets you avoid
314, hitting the reset button. Public variables
317, have more in common with the main
319, odometer in your car.
320, END